package cn.com.jsepc.rep.subject_pool;

import cn.com.jsepc.rep.comm.ConstUtil;
import cn.com.jsepc.rep.vo.SubjectPoolVO;
import com.jarp.db.DAO;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import net.sf.json.JSONArray;

public class GetTree {
    
    public String GetTree(){
        String res="";
        res=this.GetData("0");
        return res;
    }
    
    public String GetData(String subid){
        String res="";
        DAO dao=new DAO();
        String sql="select * from tb_subject_pool where subpid="+subid;
        try {
            List<SubjectPoolVO> lstResults = new ArrayList<SubjectPoolVO>(); 
            Vector v=dao.getDataSet(ConstUtil.DBNAME, sql);
            if(v!=null){
                for(int i=0;i<v.size();i++){
                  Hashtable hash=(Hashtable)v.get(i);
                  SubjectPoolVO pool=new SubjectPoolVO();
                  pool.setId((String)hash.get("SUBID"));
                  pool.setText((String)hash.get("SUBNAME"));
                  pool.setSubcode((String)hash.get("SUBCODE"));
                  pool.setSubdesc((String)hash.get("SUBDESC"));
                  pool.setSublevel((String)hash.get("SUBLEAVEL"));
                  pool.setLeaf(this.isLeaf(pool.getId()));
                  if(pool.isLeaf()==false){
                     // pool.setChildren(this.GetData(pool.getId()));
                  }
                  lstResults.add(pool);
                }
              JSONArray json = new JSONArray();
                json = JSONArray.fromObject(lstResults);
                res = json.toString();
            }
        } catch (ServletException ex) {
            Logger.getLogger(GetTree.class.getName()).log(Level.SEVERE, null, ex);
        }
        return res;
    }
    
    
    public boolean isLeaf(String subid){
        boolean b=true;
        DAO dao=new DAO();
        String sql="select count(1) from tb_subject_pool where subpid="+subid;
        try {
            String value=dao.getDataValue(ConstUtil.DBNAME, sql);
            int total=Integer.valueOf(value);
            if(total>0){
                b=false;
            }
        } catch (ServletException ex) {
            Logger.getLogger(GetTree.class.getName()).log(Level.SEVERE, null, ex);
        }
        return b;
    }
    
}
